<template>
    <div>
      <h2>修改密码</h2>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" style="width: 60%; margin: auto;" @keyup.native.enter="submitForm(ruleForm)">
          <el-form-item label="原密码" prop="oldPwd">
        <el-input v-model="ruleForm.oldPwd"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleForm)">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
    </div>
  </template>
  
  <script>
  import { updPwd } from '../api/ChangePassword';
  import{customerLoginout, userLoginout} from'../api/login'
  export default {
        data() {
          var checkAge = (rule, value, callback) => {
            if (!value) {
              return callback(new Error('原密码不能为空'));
            }
            
          };
          var validatePass = (rule, value, callback) => {
            if (value === '') {
              callback(new Error('请输入密码'));
            } else {
              if (this.ruleForm.checkPass !== '') {
                this.$refs.ruleForm.validateField('checkPass');
              }
              callback();
            }
          };
          var validatePass2 = (rule, value, callback) => {
            if (value === '') {
              callback(new Error('请再次输入密码'));
            } else if (value !== this.ruleForm.pass) {
              callback(new Error('两次输入密码不一致!'));
            } else {
              callback();
            }
          };
          return {
            ruleForm: {
              pass: '',
              checkPass: '',
              oldPwd: '',
            },
            rules: {
              pass: [
                { validator: validatePass, trigger: 'blur' }
              ],
              checkPass: [
                { validator: validatePass2, trigger: 'blur' }
              ],
            }
          };
        },
        methods: {
          submitForm(formName) {
              console.log(formName.oldPwd);
              if(formName.oldPwd==''){
                  alert("不能为空！")
                  return;
              }
              if(formName.checkPass==''){
                  alert("不能为空！")
                  return;
              }
              if(formName.pass==''){
                  alert("不能为空！")
                  return;
              }
              if(formName.pass!==formName.checkPass){
                  alert("两次输入密码不相同")
                  return;
              }
              console.log(formName.checkPass);
              updPwd({
                  oldPwd:formName.oldPwd,
                  newPwd: formName.checkPass
              }).then(res=>{
                  if(res.data.code==200){
                      this.logout();
                  }else{
                      alert("旧密码输入错误，修改失败")
                  }
              })
               
          },
          resetForm(formName) {
            this.$refs[formName].resetFields();
          },
          logout() {
        customerLoginout().then((res) => {
          if (res.data.msg == "安全退出成功") {
            this.$message.success('修改成功，请重新登录')
            sessionStorage.removeItem("authorization");
            this.$router.push("/");
          }
        });
      },
        }
      }
  </script>
  